require("../util.js");
require("../component/components.js");
require("../../jsMt/util.js");
var userInfo=JSON.parse(sessionStorage.getItem("userInfo"));
if(!userInfo){
    location.href="/loginIndex"
}
var vm = new Vue({
    el:"#blacklist",
    data:{
        pageNumber:1,
        loginUid:userInfo.id,
        friendList:{},//好友列表
        friendList1:[],
        friendList2:[]
    },
    mounted:function(){
        this.getBlackList();
    },
    methods:{
        //后退
        getBack:function(){
            history.go(-1);
        },
        bgimg:function(url){
            url = url?url:"";
            return {
                "background-image":'url("'+url+'"),url("")'
            }
        },
        /*获取查询字符对象*/
        getQueryStringArgs: function () {
            //取得查询字符串并去掉开头的问号
            var qs = (location.search.length > 0 ? location.search.substring(1) : ""),
                args = {},
                items = qs.length ? qs.split("&") : [],
                item = null,
                name = null,
                value = null,
                len = items.length;
            for (var i = 0; i < len; i++) {
                item = items[i].split("=");
                name = item[0];
                value = item[1];
                if (name.length) {
                    args[name] = value;
                }
            }
            return args;
        },
        //黑名单列表
        getBlackList:function(){
            var that=this;
            var data={
                loginUid:that.loginUid,
                pageNumber:that.pageNumber
                // loginUid:1
            };
            this.ajax({url:"/happyi/getBlackList",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0&&b.json.data){
                    that.friendList=b.json.data;
                    that.friendList=b.json.data;
                    that.friendList1=that.friendList.list;
                    if(that.friendList.pageNumber>1){
                        if(that.friendList.list.length>0) {
                            that.friendList2 = that.friendList2.concat(b.json.data.list);
                        }
                    }else{
                        that.friendList2= that.friendList1;
                    }
                    vm.loadFlag=false;
                }else{
                    that.friendList={};
                    that.friendList1=[];
                    that.friendList2=[];
                    vm.$refs.prompt.toast("获取列表失败！");
                }
            });
        },
        //删除黑名单
        removeBlack:function(friendId){
            var that=this;
            var data={
                loginUid:that.loginUid,
                friendId:friendId
                // loginUid:1
            };
            this.ajax({url:"/happyi/removeBlack",data:data,dataType:"json",method:"post"},function(b){
                if(b.json.code==0){
                    $(".friendsList-list a").removeClass("swipeleft");
                   that.getBlackList();
                }else{
                    vm.$refs.prompt.toast("获取列表失败！");
                }
            });
        }
    },
    watch:{
        friendList2:function(){
            this.$nextTick(function(){
                var that=this;
                var container = document.querySelectorAll('.friendsList-list a');
                console.log(container)
                var expansion = null; //是否存在展开的list
                for(var i = 0; i < container.length; i++){
                    var x, y, X, Y, swipeX, swipeY;
                    container[i].addEventListener('touchstart', function(event) {
                        x = event.changedTouches[0].pageX;
                        y = event.changedTouches[0].pageY;
                        swipeX = true;
                        swipeY = true ;
                        if(expansion){   //判断是否展开，如果展开则收起
                            expansion.className ="";
                        }
                    });
                    container[i].addEventListener('touchmove', function(event){
                        event.stopPropagation();
                        X = event.changedTouches[0].pageX;
                        Y = event.changedTouches[0].pageY;
                        // 左右滑动
                        if(swipeX && Math.abs(X - x) - Math.abs(Y - y) > 0){
                            // 阻止事件冒泡
                            event.stopPropagation();
                            if(X - x > 10){   //右滑
                                event.preventDefault();
                                this.className = "";    //右滑收起
                                //console.log("右滑")
                            }
                            if(x - X > 10){   //左滑
                                event.preventDefault();
                                this.className = "swipeleft";   //左滑展开
                                //console.log("左滑")
                                expansion = this;
                            }
                            swipeY = false;
                        }
                        // 上下滑动
                        if(swipeY && Math.abs(X - x) - Math.abs(Y - y) < 0) {
                            swipeX = false;
                        }
                    });
                }
            });
        }
    }
});